package controllers

import (
	"Gin_dome/models"
	"Gin_dome/uitls"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"log"
	"net/http"
)

func Index(c *gin.Context) {
	//fmt.Println(c.Request.RequestURI)  // 这个Request.RequestURL 是路径和查询字符串一起的
	url := c.Request.URL.Path // 这个Request.URL.Path 只有路径
	//获取url中的类型，如首页， python
	name := c.Request.URL.Path[6:]

	client := &http.Client{}
	//提交请求
	reqest, _ := http.NewRequest("GET", "https://chp.shadiao.app/api.php", nil)

	html, _ := client.Do(reqest)
	n, _ := ioutil.ReadAll(html.Body)
	content := string(n)
	//log.Println(content)

	page_num := c.DefaultQuery("page", "1")
	log.Println(name, page_num, c.Request.URL.Path)

	names := name
	if len(name) <= 0 {
		name = "all"
	} else {
		//数据切割，
		name = name[:len(name)-1]
		names = name
		dicts := uitls.QueryString
		//判断该url类型是否有
		if len(dicts[name]) <= 0 {

			c.Redirect(302, "/blog/404/404.html")
		}
		name = dicts[name]
	}

	blog := &models.Blog{}
	item, blogpage, _ := blog.Aaa(name, page_num)
	//log.Println(blogpage, *blogpag, reflect.TypeOf(blogpage))
	c.HTML(200, "index.html", gin.H{
		"name":     names,
		"item":     item,
		"blogpage": blogpage,
		"url":      url,
		"content":  content,
	})
}

func Python(c *gin.Context) {
	Index(c)
	//fmt.Println(c.Request.RequestURI)
	//blog := &models.Blog{}
	//item, _ := blog.Aaa("Python")

	//c.HTML(200, "index.html", gin.H{
	//	"name": "python",
	//	"item": item,
	//})
}
func Golang(c *gin.Context) {
	Index(c)
	//fmt.Println(c.Request.RequestURI)
	//blog := &models.Blog{}
	//item, _ := blog.Aaa("Golang")
	//c.HTML(200, "index.html", gin.H{
	//	"name": "golang",
	//	"item": item,
	//})
}
func C(c *gin.Context) {
	Index(c)
	//fmt.Println(c.Request.RequestURI)
	//blog := &models.Blog{}
	//item, _ := blog.Aaa("C++")
	//c.HTML(200, "index.html", gin.H{
	//	"name": "c++",
	//	"item": item,
	//})
}
func Database(c *gin.Context) {
	Index(c)
	//fmt.Println(c.Request.RequestURI)
	//blog := &models.Blog{}
	//item, _ := blog.Aaa("数据库")
	//c.HTML(200, "index.html", gin.H{
	//	"name": "database",
	//	"item": item,
	//})
}

func DataSal(c *gin.Context) {
	Index(c)
	//fmt.Println(c.Request.RequestURI)
	//
	//blog := &models.Blog{}
	//item, _ := blog.Aaa("数据结构与算法")
	//c.HTML(200, "index.html", gin.H{
	//	"name": "data_alg",
	//	"item": item,
	//})
}
func Rests(c *gin.Context) {
	Index(c)
	//fmt.Println(c.Request.RequestURI)
	//blog := &models.Blog{}
	//item, _ := blog.Aaa("其他")
	//c.HTML(200, "index.html", gin.H{
	//	"name": "rests",
	//	"item": item,
	//})
}

func NoResponse(c *gin.Context) {
	c.HTML(200, "404.html", gin.H{})
}
